package com.vincce.fitness.entity;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: henry
 * Date: 8/7/13
 * Time: 9:56 AM
 * To change this template use File | Settings | File Templates.
 */
public class Page<T> {
	//默认分页条数
	private static int DEFAULT_SIZE = 10;
	//当前页数
	private int pageNo = 1;
	//默认一页展示数据
	private int pageSize = DEFAULT_SIZE;
	//查询开始出
	private int index = -1;
	//查询结果存放处
	private List<T> result = new ArrayList<T>();
	//查询总条数
	private long totalCount = -1;
	//查询条件放置处
	private LinkedHashMap<String, Object> filter = new LinkedHashMap<String, Object>();
	//升序
	public static final String ASC = "asc";
	//降序
	public static final String DESC = "desc";
	//===构造参数===//
	public Page() {

	}

	public Page(int pageSize) {
		this.pageSize = pageSize;
	}
	//-- 分页参数访问函数 --//
	/** 获取当前页数，默认从一开始**/
	public int getPageNo () {
		return pageNo;
	}

	/**设置当前页数，低于1时，自动调整为1 **/
	public void setPageNo (final int pageNo) {
		this.pageNo = pageNo;
		if (pageNo < 1) {
			this.pageNo = 1;
		}
	}

	/**返回Page对象自身的setPageNo函数，可用于连续设置**/
	public Page<T> pageNo (final int thePageNo) {
		setPageNo(thePageNo);
		return this;
	}

	/**获得每页的记录数量，默认为10 **/
	public int getPageSize() {
		return pageSize;
	}

	/** 设置每页的记录数量**/
	public void setPageSize (final int pageSize) {
		this.pageSize = pageSize;
	}

	/**返回Page对象自身的setPageSize函数 **/
	public Page<T> pageSize (final int thePageSize) {
		setPageSize(thePageSize);
		return this;
	}
	/**根据pageNo和pageSize计算当前页第一条记录 **/
	public int getFirst () {
		return ((pageNo - 1)*pageSize) + 1;
	}

	public int getIndex () {
		return index == -1?(pageNo - 1)*pageSize:index;
	}

	public int getStart() {
		return getIndex();
	}

	public void setIndex (int index) {
		this.index = index;
	}

	public void setI (int index) {
		this.index = index;
	}

	/**获得页内的记录列表 **/
	public List<T> getResult () {
		return result;
	}
	/**
	 * 设置页内的记录列表.
	 */
	public void setResult (final  List<T> result) {
		this.result = result;
	}
	/**
	 * 获得总记录数, 默认值为-1.
	 */
	public long getTotalCount () {
		return totalCount;
	}
	/**
	 * 设置总记录数.
	 */
	public void setTotalCount (final long totalCount) {
		this.totalCount = totalCount;
	}

	/**
	 * 根据pageSize与totalCount计算总页数, 默认值为-1.
	 */
	public long getTotalPages () {
		if (totalCount < 0) {
			return -1;
		}
		long count = totalCount / pageSize;
		if (totalCount % pageSize > 0) {
			count ++;
		}
		return count;
	}

	/**
	 * 是否还有下一页.
	 */
	public boolean isHasNext () {
		return (pageNo + 1 <= getTotalPages());
	}

	/**
	 * 取得下页的页号, 序号从1开始.
	 * 当前页为尾页时仍返回尾页序号.
	 */
	public int getNextPage () {
		if (isHasNext()) {
			return pageNo + 1;
		} else {
			return pageNo;
		}
	}

	/**
	 * 是否还有上一页.
	 */
	public boolean isHasPre () {
		return (pageNo - 1 >= 1);
	}

	/**
	 * 取得上页的页号, 序号从1开始.
	 * 当前页为首页时返回首页序号.
	 */
	public int getPrePage () {
		if (isHasPre()) {
			return pageNo - 1;
		} else {
			return pageNo;
		}
	}

	public LinkedHashMap<String, Object> getFilter () {
		return filter;
	}

	public void setFilter (LinkedHashMap<String, Object> filter) {
		this.filter = filter;
	}

	public void setF (LinkedHashMap<String, Object> filter) {
		this.filter = filter;
	}

	protected String orderBy = null;//time desc,ereer asc

	protected String order = null;

	/**
	 * 获得排序字段,无默认值. 多个排序字段时用','分隔.
	 */
	public String getOrderBy() {
		return orderBy;
	}

	/**
	 * 设置排序字段,多个排序字段时用','分隔.
	 */
	public void setOrderBy(final String orderBy) {
		this.orderBy = orderBy;
	}

	/**
	 * 返回Page对象自身的setOrderBy函数,可用于连续设置。
	 */
	public Page<T> orderBy(final String theOrderBy) {
		setOrderBy(theOrderBy);
		return this;
	}

	/**
	 * 是否已设置排序字段,无默认值.
	 */
	public boolean isOrderBySetted() {
		return orderBy != null && orderBy.trim().length() > 0;
	}


	public String getOrder() {
		return order;
	}

	public void setOrder(final String order) {
		this.order = order;
	}
	/**
	 * 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置,序号从1开始.
	 */
	public int getBegin() {
		return getIndex() + 1;
	}

	public int getEnd() {
		return  (getIndex() == -1 ? 0 : getIndex()) + getPageSize();
	}
}
